package com.ny.zmb.openapi.ysutil;

import com.alibaba.fastjson.JSONObject;
import com.ny.zmb.openapi.entity.PmApiLog;
import com.ny.zmb.openapi.enums.CodeEnum;
import com.ny.zmb.openapi.service.IPmApiLogService;
import com.ny.zmb.openapi.ysutil.base.BaseBizReqCommand;
import com.ny.zmb.openapi.ysutil.base.EYinShengBase;
import com.ny.zmb.openapi.ysutil.base.HeadResponseCommand;
import com.ny.zmb.openapi.ysutil.base.MerchantHeadRequestCommand;
import com.ny.zmb.openapi.ysutil.common.ServiceEnum;
import com.ny.zmb.openapi.ysutil.common.YsPayConstant;
import com.ny.zmb.openapi.ysutil.publicInterface.QueryAmountReqCommand;
import com.ny.zmb.openapi.ysutil.scancode.BackCodePayReqCommand;
import com.ny.zmb.openapi.ysutil.scancode.BackCodePayRespCommand;
import com.ny.zmb.openapi.ysutil.utils.JsonParser;
import com.ny.zmb.openapi.ysutil.utils.YinshengLogger;
import org.apache.commons.lang.NullArgumentException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

@Service
public class YsHttpUtil extends EYinShengBase {

    @Resource
    private IPmApiLogService logService;

    /**
     * @param WALLET_PAY_BARCODE
     * @param ENV_TEST
     * @param MERCHANT_PRX
     * @param MERCHANT_PRX_PWD
     * @param command
     * @return
     */
    public JSONObject send(ServiceEnum WALLET_PAY_BARCODE, String ENV_TEST, String MERCHANT_PRX, String MERCHANT_PRX_PWD, String serviceNo, BaseBizReqCommand command) {
        YinShengClient yinShengClient = new YinShengClient(WALLET_PAY_BARCODE, ENV_TEST, MERCHANT_PRX, MERCHANT_PRX_PWD);
        MerchantHeadRequestCommand headRequestCommand = new MerchantHeadRequestCommand().setSrcMerchantNo(SRC_MERCHANT_NO);
        headRequestCommand.setServiceNo(serviceNo);
        JSONObject jsonObject = yinShengClient.doAction(headRequestCommand, command);
        String code = jsonObject.getString("code");
        if (!"SYS000".equals(code)) {
            throw new RuntimeException(jsonObject.getString("msg"));
        }
        return jsonObject;
    }


    /**
     * @param serviceEnum    枚举
     * @param env            环境
     * @param merchantPrx    私钥
     * @param merchantPrxPwd 私钥密码
     * @param serviceNo      服务编码:部门登记
     * @param srcMerchantNo  发起方商户号 示例值:826392148160023
     * @param bizReqJson     业务请求参数的集合，JSON格式
     * @return Json字符串
     */

    public JSONObject sendBase(ServiceEnum serviceEnum, String env, String merchantPrx, String merchantPrxPwd, String serviceNo, String srcMerchantNo, Object bizReqJson) {
        env = env == null ? "prd" : env;
        merchantPrx = merchantPrx == null ? MERCHANT_PRX : merchantPrx;
        merchantPrxPwd = merchantPrxPwd == null ? MERCHANT_PRX_PWD : merchantPrxPwd;
        serviceNo = serviceNo == null ? serviceEnum.getApiName() : serviceNo;
        srcMerchantNo = srcMerchantNo == null ? SRC_MERCHANT_NO : srcMerchantNo;

        YinShengClient yinShengClient = new YinShengClient(serviceEnum, env, merchantPrx, merchantPrxPwd);
        PmApiLog pmApiLog = new PmApiLog(serviceEnum.getPrdUrl() + serviceEnum.getApiName(), bizReqJson.toString());
        logService.save(pmApiLog);
        MerchantHeadRequestCommand headRequestCommand = new MerchantHeadRequestCommand().setSrcMerchantNo(srcMerchantNo);
        headRequestCommand.setServiceNo(serviceNo);
        JSONObject jsonObject = yinShengClient.baseAction(headRequestCommand, bizReqJson);
        pmApiLog.setReturnObj(jsonObject.toJSONString());
        logService.updateById(pmApiLog);
        return resultAnalysis(jsonObject);
    }

    public JSONObject sendPlus(ServiceEnum serviceEnum, Object obj) {
        YinShengClient yinShengClient = new YinShengClient(serviceEnum, "prd", MERCHANT_PRX, MERCHANT_PRX_PWD);
        MerchantHeadRequestCommand headRequestCommand = new MerchantHeadRequestCommand().setSrcMerchantNo(SRC_MERCHANT_NO);
        headRequestCommand.setServiceNo(serviceEnum.getApiName());
        PmApiLog pmApiLog = new PmApiLog(serviceEnum.getPrdUrl() + serviceEnum.getApiName(), obj.toString());
        logService.save(pmApiLog);
        JSONObject jsonObject = yinShengClient.baseAction(headRequestCommand, obj);
        pmApiLog.setReturnObj(jsonObject.toJSONString());
        logService.updateById(pmApiLog);
        return resultAnalysis(jsonObject);
    }

    /**
     * 空数据默认填充
     */
    public JSONObject sendPlus(ServiceEnum serviceEnum, String ENV_TEST, String merchantPrx, String merchantPrxPwd, String serviceNo, BaseBizReqCommand command) {
        ENV_TEST = ENV_TEST == null ? "prd" : ENV_TEST;
        merchantPrx = merchantPrx == null ? MERCHANT_PRX : merchantPrx;
        merchantPrxPwd = merchantPrxPwd == null ? MERCHANT_PRX_PWD : merchantPrxPwd;
        serviceNo = serviceNo == null ? serviceEnum.getApiName() : serviceNo;
        YinShengClient yinShengClient = new YinShengClient(serviceEnum, ENV_TEST, merchantPrx, merchantPrxPwd);
        MerchantHeadRequestCommand headRequestCommand = new MerchantHeadRequestCommand().setSrcMerchantNo(SRC_MERCHANT_NO);
        headRequestCommand.setServiceNo(serviceNo);
        PmApiLog pmApiLog = new PmApiLog(serviceEnum.getPrdUrl() + serviceEnum.getApiName(), command.toString());
        logService.save(pmApiLog);
        JSONObject jsonObject = yinShengClient.baseAction(headRequestCommand, command);
        pmApiLog.setReturnObj(jsonObject.toJSONString());
        logService.updateById(pmApiLog);
        return jsonObject;
    }

    public JSONObject resultAnalysis(JSONObject jsonObject) throws NullArgumentException {
        String code = jsonObject.getString("code");
        if (code == null || code.isEmpty()) {
            throw new NullArgumentException("code cannot be null or empty");
        }
        switch (code) {
            case "SYS000":
                return jsonObject.getJSONObject("bizResponseJson");
            case "SYS001":
                return jsonObject.getJSONObject("网关受理中,请稍后重试");
            case "SYS003":
            case "SYS004":
            case "SYS005":
                return jsonObject.getJSONObject("msg");
            default:
                return jsonObject;
        }
    }
}
